﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using ProduceBasicData.Api.Application.Command.WarehouseManage;
using ProduceBasicData.Domain.WarehouseManage;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;
using ProduceBasicData.Api.Services;

namespace ProduceBasicData.Api.Application.Handler.WarehouseManage
{
    /// <summary>
    /// 仓库录入处理类
    /// </summary>
    public class CreateWarehouseHandler : IRequestHandler<CreateWarehouseCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<Warehouse> _warehouseRepository;
        private readonly IMapper _mapper;
        private readonly ICodeRuleService codeRuleService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="warehouseRepository"></param>
        /// <param name="mapper"></param>
        public CreateWarehouseHandler(IBaseRespository<Warehouse> warehouseRepository, IMapper mapper,
            ICodeRuleService codeRuleService)
        {
            _warehouseRepository = warehouseRepository;
            _mapper = mapper;
            this.codeRuleService = codeRuleService;
        }
        /// <summary>
        /// 处理仓库录入请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(CreateWarehouseCommand request, CancellationToken cancellationToken)
        {
            try
            {
                //将命令转换为领域模型
                var warehouse = _mapper.Map<Warehouse>(request);

                //编码
                if (string.IsNullOrEmpty(warehouse.Code))
                {
                    var code = await codeRuleService.AutoGenerateCodeAsync("ProduceBasicData.Domain.WarehouseManage.Warehouse");
                    if (!string.IsNullOrEmpty(code))
                    {
                        warehouse.Code = code;
                    }
                    else
                    {
                        warehouse.Code = $"House{DateTime.Now:yyyyMMddHHmmss}";
                    }
                }

                //判断仓库是否存在
                var warehouseExist = await _warehouseRepository.GetAll().AnyAsync(x => x.Name == warehouse.Name);
                if (warehouseExist != false)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnum.Failed,
                        Msg = "仓库已存在"
                    };
                }
                //保存仓库
                var result = await _warehouseRepository.AddAsync(warehouse);
                if (result > 0)
                {
                    return new ApiResult<int>
                    {
                        Data = result,
                        Code = ApiEnum.Success,
                        Msg = "仓库录入成功"
                    };
                }
                else
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnum.Failed,
                        Msg = "仓库录入失败"
                    };
                }
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.Failed,
                    Msg = ex.Message
                };
            }
        }
    }
}
